import axios from 'axios'

// import { throttleAdapterEnhancer } from 'axios-extensions'
import { closeToast, showLoadingToast } from 'vant'

// 创建axios实例
const service = axios.create({
  // 服务接口请求
  baseURL: import.meta.env.VITE_APP_BASE_API,
  // 超时设置
  timeout: 6000,
  // adapter: throttleAdapterEnhancer(axios.defaults.adapter, { threshold: 1 * 1500 }), // default value = 1000
  headers: {
    'Content-Type': 'application/json;charset=utf-8',
    'tenant-id': 1,
    'Authorization': 'Bearer test1'
  }
})

// http请求拦截
service.interceptors.request.use(
  // config:该参数表示当前请求的配置对象
  (config) => {
    showLoadingToast({
      message: '加载中...',
      forbidClick: true
    })
    return config
  },
  (err) => {
    closeToast() // 请求失败关闭loadding
    return Promise.reject(err) // 将错误消息挂到promise的失败函数上
  }
)

service.interceptors.response.use(
  (response) => {
    closeToast() // 响应成功关闭loadding
    return response.data // 这里的response就是请求成功后的res , response.data即是请求成功后回调函数内的参数res.data
  },
  (err) => {
    closeToast() // 响应失败关闭loadding
    return Promise.reject(err) // 将错误消息挂到promise的失败函数上
  }
)

// 公钥
const rsaPubKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHK5jMHKtPFhhtTp5rY9xinDDaaF0QE1'
  + 'oDPGl2/W0AmNQbvh0IWWWfnW9uSOsgRfGG2jFopcUaJ26216SDyHOZ5MF97OiDsAga8ZPHRoWXOy06dGB/Vo'
  + '3519l22T7jfC475pC37IRA25uoa2DPjAL+j0LzTg89xUvgQnGQX1Ab6wIDAQAB'

export default service
